/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-25
 * Function :  implement hashtable
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/


#ifndef AX_HASHTABLE_H
#define AX_HASHTABLE_H
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>

typedef struct ax_hashtable ax_hashtable;
typedef struct ax_hashtable_entry ax_hashtable_entry;
typedef enum {
    HT_NONE = 0,
    HT_KEY_CONST = 1,       /*key alloc/free outside*/
    HT_VALUE_CONST = 2,     /*value alloc/free outside*/
    HT_NO_AUTORESIZE = 4    /*no auto resize the table*/
} AX_HT_FLAGS;

ax_hashtable* ax_hashtable_create(AX_HT_FLAGS flags);
void          ax_hashtable_destory(ax_hashtable*h);
void          ax_hashtable_insert(ax_hashtable*d,const void* key, size_t key_size, const void* value, size_t value_size);
void*         ax_hashtable_get(ax_hashtable*d,const char*key, size_t key_size, size_t *value_size);
bool          ax_hashtable_remove(ax_hashtable*d,const char*key, size_t key_size);
bool          ax_hashtable_contains(ax_hashtable*d,const char* key, size_t key_size);
uint32_t      ax_hashtable_size(ax_hashtable* table);
void          ax_hashtable_clear(ax_hashtable* table);

/* should not call by user direct */
void* ax_hashtable_entry_get_key(void* entry);
void* ax_hashtable_entry_get_value(void* entry);
void* ax_hashtable_get_head(ax_hashtable* table);
#define ax_hashtable_foreach(key, value, table) \
for(ax_list_node* __node__=ax_hashtable_get_head(table);\
    __node__!=(key=ax_hashtable_entry_get_key(__node__), value=ax_hashtable_entry_get_value(__node__), NULL);\
    __node__=__node__->next)


/*
    example:

    ax_hasehtable* table = ax_hashtable_create(HT_KEY_CONST|HT_VALUE_CONST);
    //insert
    ax_hashtable_insert(table, "key", 3, "val", 3);
    //destory
    ax_hashtable_destory(table);
*/


#endif // AX_HASHTABLE_H
